package com.lw.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.acl.entity.Role;
import com.lw.acl.entity.User;
import com.lw.acl.entity.UserRole;
import com.lw.acl.mapper.UserMapper;
import com.lw.acl.service.RoleService;
import com.lw.acl.service.UserRoleService;
import com.lw.acl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService  roleService;

    //根据用户名查找用户信息，并查询当前用户拥有的角色信息
    @Override
    public  User selectByUsername(String username) {
       /* QueryWrapper<User> wrapper = new QueryWrapper<User>().eq( "username", username );
        User user = baseMapper.selectOne( wrapper );
        //查询用户角色中间表得到角色id
        QueryWrapper<UserRole> userRoleWrapper = new QueryWrapper<>();
        userRoleWrapper.eq( "user_id",user.getId() );
        List<UserRole> userRoles = userRoleService.list( userRoleWrapper );
        List<String> roleIds = new ArrayList<>();
        userRoles.forEach( userRole -> {
            roleIds.add( userRole.getRoleId() );
        } );
        //通过角色id查询角色信息保存到用户信息中
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.in( "role_id",roleIds); //有问题
        List<Role> roleList = roleService.list( roleQueryWrapper );
        user.setRoles( roleList );
        return user*/;
        return baseMapper.selectByUsername( username );
    }

}
